package com.kotlin.library.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.Base64;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.squareup.picasso.Picasso;

import java.io.ByteArrayOutputStream;

public class ToolImage {



	/**
	 * 图片加载状态监听
	 */
	public interface OnLoadingListener {

		void onResourceReady(Bitmap resource);
	}

	/**
	 * 下载图片
	 * 有缓存
	 * 有返回监听
	 */
	public static void setImageLoader(final ImageView imageView, String imageUrl, Context context, int defaultImageResId, int errorImageResId,final OnLoadingListener listener) {
		if(context == null) return;

		if(imageView != null) {
			imageView.setVisibility(View.VISIBLE);
		}
		Glide.with(context)
				.load(imageUrl)
				.asBitmap()
				.error(errorImageResId)
				.placeholder(defaultImageResId)
				.into(new SimpleTarget<Bitmap>() {

					@Override
					public void onLoadStarted(Drawable placeholder) {
						if(imageView!=null) {
							//ToolAlert.toastLong("onLoadStarted");
							imageView.setImageDrawable(placeholder);
						}
					}

					@Override
					public void onLoadFailed(Exception e, Drawable errorDrawable) {
						if(imageView!=null) {
							//ToolAlert.toastLong("onLoadFailed");
							imageView.setImageDrawable(errorDrawable);
						}
					}


					@Override
					public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
						if(imageView!=null) {
							//ToolAlert.toastLong("onResourceReady");

							//imageView.setImageBitmap(resource);
							imageView.setImageBitmap(ImageUtils.compressBySampleSize(resource,2));
							if(listener != null) {
								listener.onResourceReady(resource);
							}
						}
					}
				});



	}


	/**
	 * 下载图片
	 * 有缓存
	 */
	public static void setImageLoader(ImageView imageView, String imageUrl, Context context, int defaultImageResId, int errorImageResId) {
		//if(context == null) return;
		//
		//if(imageView != null) {
		//	imageView.setVisibility(View.VISIBLE);
		//
		//}
		//
		//
		//
		//Glide.with(context)
		//		.load(imageUrl)
		//		.asBitmap()
		//		.error(errorImageResId)
		//		.placeholder(defaultImageResId)
		//		.into(new SimpleTarget<Bitmap>() {
		//
		//			@Override
		//			public void onLoadStarted(Drawable placeholder) {
		//				if(imageView!=null) {
		//					//ToolAlert.toastLong("onLoadStarted");
		//				imageView.setImageDrawable(placeholder);
		//				}
		//			}
		//
		//			@Override
		//			public void onLoadFailed(Exception e, Drawable errorDrawable) {
		//				if(imageView!=null) {
		//					//ToolAlert.toastLong("onLoadFailed");
		//					imageView.setImageDrawable(errorDrawable);
		//				}
		//			}
		//
		//
		//			@Override
		//			public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
		//				if(imageView!=null) {
		//					//ToolAlert.toastLong("onResourceReady");
		//					imageView.setImageBitmap(ImageUtils.compressBySampleSize(resource,2));
		//				    //imageView.setImageBitmap(resource);
		//				}
		//			}
		//		});


		setImageLoader(imageView,imageUrl,context,defaultImageResId,errorImageResId,null);

	}

	public static void setImageLoaderWithPicasso(ImageView imageView, String imageUrl,
			Context context, int defaultImageResId, int errorImageResId) {
		if(context == null) return;

		if (TextUtils.isEmpty(imageUrl)) imageUrl = null;

		Picasso.with(context)
				.load(imageUrl)
				.error(errorImageResId)
				.placeholder(defaultImageResId)
				.into(imageView);


	}

	//}
	/**
	 * 位图转Base64
	 *
	 * @param bitmap
	 * @return
	 */
	public static String bitmapToString(Bitmap bitmap) {

		// 将Bitmap转换成字符串

		String string = null;

		ByteArrayOutputStream bStream = new ByteArrayOutputStream();

		bitmap.compress(Bitmap.CompressFormat.JPEG, 50, bStream);

		byte[] bytes = bStream.toByteArray();

		string = Base64.encodeToString(bytes, Base64.DEFAULT);

		return string;

	}




}
